package com.zorn.core.provider;

import static com.zorn.platform.global.Setting.getBooleanValue;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.ContextLoader;

import com.alibaba.fastjson.JSONObject;
import com.zorn.platform.global.Setting;

@SuppressWarnings("unchecked")
public class ActionSupportProvider {

	// Properties
	private Object jsonObject;

	private Map<String, Object> params;

	public static final String JSON_STRING = "json", JSON_ARRAY = "json", JSON_OBJECT = "json", JSON_RESULT = "json", JSON_EXCEPTION = "json";

	// Methods
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpSession getSession(boolean create) {
		return getRequest().getSession(create);
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public ServletContext getApplication() {
		return getServletContext();
	}

	public String getRealPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public Object getBean(Class cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Object getBean(String cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public boolean isPaging() {
		String start = getParameter("start", ""), limit = getParameter("limit", "");
		boolean result = start.matches("") && limit.matches("");

		// FIXME 放到参数拦截器去转换 这里只实现判断是否分页
		if (!result && getBooleanValue("Zorn.param.query.paging.AutoCorrectPagingParam", false)) {
			Map<String, Object> params = getParameterMap();
			params.put("start", Setting.getString("Zorn.param.query.paging.start", "0"));
			params.put("limit", Setting.getString("Zorn.param.query.paging.limit", "30"));
		}
		return result;
	}

	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	public String getParameter(String name, String def) {
		String param = getRequest().getParameter(name);

		if (param == null) return def;

		return param;
	}

	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	public Map<String, Object> getParameterMap() {
		return params;
	}

	public void setParameterMap(Map<String, Object> params) {
		this.params = params;
	}

	public JSONObject getParameterJSONObject() {
		return new JSONObject(getParameterMap());
	}

	// Getters and Setters
	public Object getJSONObject() {
		return jsonObject;
	}

	public void setJSONObject(Object obj) {
		this.jsonObject = obj;
	}

	public void setJSONPagingResult(Object data, int total) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", data);
		result.put("total", total);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(boolean successful) {
		setJSONProcessResult(successful, null);
	}

	public void setJSONProcessResult(boolean successful, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", successful);
		if (message != null) result.put("message", message);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(Map<String, Object> result) {
		this.jsonObject = result;
	}

}
